package com.example.testdemo.demos.web.Service.Impl;

import com.example.testdemo.demos.web.Config.JwtInfo;
import com.example.testdemo.demos.web.Config.Result;
import com.example.testdemo.demos.web.Config.ResultEnum;
import com.example.testdemo.demos.web.Dao.MerchantDao;
import com.example.testdemo.demos.web.Entity.Merchant;
import com.example.testdemo.demos.web.Service.MerchantService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Date;

@Service
public class MerchantServiceImpl implements MerchantService {

    @Resource
    private MerchantDao merchantDao;

    /**
     * 添加商家
     */
    @Override
    public Result addMerchant(Merchant merchant, JwtInfo jwtInfo){
        Result result =new Result();
        try {
            Merchant merchant1 =merchantDao.selectById(jwtInfo.getUserId());
            if (merchant1==null){
                merchant.setMerchantId(jwtInfo.getUserId());
                if (merchant.getMerchantPhone()==null){
                    merchant.setMerchantPhone(jwtInfo.getUser().getCellPhone());
                }
                //营业状态 离线
                merchant.setBusinessStatus("L");
                //入住平台时间
                merchant.setPlatformEntryTime(new Date());
                //审核状态 神审核中
                merchant.setAuditStatus("Z");
                //审核状态名称
                merchant.setAuditStatusName("审核中");
                //账户状态 账户申诉中
                merchant.setAccountStatus("4");
                int res = merchantDao.insert(merchant);
                if (res==1){
                    result.setCode(ResultEnum.SUCCESS.getCode());
                    result.setMsg("商户注册成功");
                }else {
                    result.setCode(ResultEnum.DB_UPDATE_FAILURE.getCode());
                    result.setMsg("商户注册失败");
                }
            }else {
                result.setCode(ResultEnum.FAILED_VISA_INSPECTION.getCode());
                result.setMsg("账户已存在！");
            }
        }catch (Exception e){
         result.setCode(ResultEnum.UNKNOWNERROR.getCode());
         result.setMsg(e);
        }
        return result;
    }

    /**
     * 商家审核/冻结/操作
     */
    @Override
    public Result processMerchant(Merchant merchant, JwtInfo jwtInfo){
        Result result =new Result();
        try {
            Boolean pass =Arrays.stream(jwtInfo.getPurview()).anyMatch(s -> s.equals("admin") || s.equals("superAdmin"));
            if (pass){
                merchant.setAuditTime(new Date());
                merchant.setAuditor(jwtInfo.getUser().getUserName());
                merchant.setAuditorId(jwtInfo.getUserId());
                int res = merchantDao.updateById(merchant);
                if (res==1){
                    result.setCode(ResultEnum.SUCCESS.getCode());
                    result.setMsg("商户信息操作成功");
                }else {
                    result.setCode(ResultEnum.DB_UPDATE_FAILURE.getCode());
                    result.setMsg("商户信息操作失败");
                }
            }else {
                result.setCode(ResultEnum.FAILED_VISA_INSPECTION.getCode());
                result.setMsg("您暂无权限！");
            }
        }catch (Exception e){
            result.setCode(ResultEnum.UNKNOWNERROR.getCode());
            result.setMsg(e);
        }
        return result;
    }
}
